def call(Map params) {
    def name = params.name ?: 'Anonymous'
    echo "测试发送Teams消息，名字：${name}"
}

def validateParams(Map params) {
    ['branchName', 'jobName', 'buildNumber', 'buildUrl', 'channel'].each {
        if (!params[it]) {
            throw new IllegalArgumentException("Missing required parameter: ${it}")
        }
    }
}

def constructMessageDetails(Map params) {
    def branchName = params.branchName
    def buildTime = new Date().format("yyyy-MM-dd HH:mm:ss")
    def gitCommitMessage = sh(script: "git log -1 --pretty=%B", returnStdout: true).trim()
    def jobName = java.net.URLDecoder.decode("${params.jobName} [${params.buildNumber}]", "UTF-8")
    def status = currentBuild.resultIsBetterOrEqualTo('SUCCESS') ? 'SUCCESS' : 'FAILURE'
    def url = "<${params.buildUrl}|Click here to view the build>"

    return [branchName: branchName, buildTime: buildTime, gitCommitMessage: gitCommitMessage, jobName: jobName, status: status, url: url]
}

def constructSlackMessage(Map messageDetails) {
    def message = new StringBuilder()
    message.append("Job Name: '${messageDetails.jobName}'\n")
    message.append("Status: ${messageDetails.status}\n")
    message.append("Branch: ${messageDetails.branchName}\n")
    message.append("Build Time: ${messageDetails.buildTime}\n")
    message.append("Commit Message: ${messageDetails.gitCommitMessage}\n")
    message.append("URL: ${messageDetails.url}")
    return message.toString()
}

def sendSlackMessage(String status, String message, String channel) {
    slackSend(
        color: status == 'SUCCESS' ? 'good' : 'danger',
        channel: channel,
        message: message
    )
}
